<?php
/**
 * Created by PhpStorm.
 * User: joe
 * Date: 2019-12-12
 * Time: 23:52
 */

namespace app\common\utils;

/**
 * 字符串工具类
 * Class StringUtil
 * @package app\common\utils
 */
class StringUtil extends BaseUtil
{

    static $_self = null;

    public static function getInstance()
    {
        if (empty(self::$_self)) {
            self::$_self = new StringUtil();
        }
        return self::$_self;
    }

    /**
     *  将下划线命名转换为驼峰式命名
     * @param $str
     * @param bool $ucFirst
     * @return string
     */
    public function convertUnderline($str, $ucFirst = false)
    {
        while (($pos = strpos($str, '_')) !== false)
            $str = substr($str, 0, $pos) . ucfirst(substr($str, $pos + 1));

        return $ucFirst ? ucfirst($str) : $str;
    }

    /**
     * 判断字符串类型的变量true还是false
     * @param $val
     * @param bool $return_null
     * @return bool|mixed|null
     */
    function is_true($val, $return_null = false)
    {
        $boolVal = (is_string($val) ? filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) : (bool)$val);
        return ($boolVal === null && !$return_null ? false : $boolVal);
    }

}